package org.jtzc.springaiagent.agent.planning;

import org.jtzc.springaiagent.agent.BaseAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wu chuang
 * @description
 */
@Service
public class PlanningAgent implements BaseAgent {
    private final BaseAgent planner;
    private final BaseAgent executor;

    @Autowired
    public PlanningAgent(
            @Qualifier("plannerAgent") BaseAgent planner,
            @Qualifier("executorAgent") BaseAgent executor) {

        this.planner = planner;
        this.executor = executor;
    }

    @Override
    public AgentResponse execute(AgentRequest request) {
        // 第一步：创建计划
        AgentRequest planRequest = new AgentRequest(
                "Create a step-by-step plan to: " + request.prompt(),
                request.context()
        );

        AgentResponse planResponse = planner.execute(planRequest);

        // 第二步：执行计划
        AgentRequest executeRequest = new AgentRequest(
                "Execute the following plan: " + planResponse.content(),
                mergeContexts(request.context(), planResponse.metadata())
        );

        return executor.execute(executeRequest);
    }

    private Map<String, Object> mergeContexts(
            Map<String, Object> original,
            Map<String, Object> additions) {

        Map<String, Object> merged = new HashMap<>(original);
        merged.putAll(additions);
        return merged;
    }
}

